import { pinyin } from 'pinyin-pro'

//处理值班表
export const transformSchedule = (daysObj) => {
  const transformedArray = []
  const index = ['one', 'two', 'three', 'four', 'five']
  const dayIndex = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
  dayIndex.forEach((day) => {
    const dayData = {}
    for (let i = 1; i <= 5; i++) {
      const key = i
      dayData[key] = daysObj[day][index[i - 1]]
      // .filter((item) => item !== '无')
    }
    transformedArray.push({
      ...dayData
    })
  })
  return transformedArray
}

// 处理个人表 01互转tf
export const changeDutySelectList = (dutySelectList) => {
  const newDutySelectList = []
  for (let i = 0; i < dutySelectList.length; i++) {
    if (typeof dutySelectList[i] === 'boolean') {
      newDutySelectList.push(dutySelectList[i] ? 1 : 0)
    } else if (typeof dutySelectList[i] === 'number') {
      newDutySelectList.push(dutySelectList[i] === 1)
    }
  }
  return newDutySelectList
}

//筛选获取所有名字
export const filterNames = (daysObj) => {
  const uniqueValues = new Set()
  const result = []
  daysObj.forEach((obj) => {
    for (const key in obj) {
      obj[key].forEach((value) => {
        if (value !== '' && value !== '无' && !uniqueValues.has(value)) {
          uniqueValues.add(value)
          result.push(value)
        }
      })
    }
  })
  result.sort(sortByFirstCharPinyin)
  result.unshift('无')
  result.unshift('空')
  return result
}

//处理按时段看空闲人
export const splitArrayEvenly = (arr, numChunks) => {
  if (arr == []) return []
  const chunkSize = Math.floor(arr.length / numChunks)
  const extraItems = arr.length % numChunks
  let chunks = Array.from({ length: numChunks }, () => [])
  for (var i = 0, offset = 0; i < numChunks; i++) {
    chunks[i] = arr.slice(offset, offset + chunkSize)
    offset += chunkSize
  }
  for (let i = 0; i < extraItems; i++) {
    chunks[i].push(arr[offset + i])
  }
  return chunks
}

/*过滤掉status */
export const filterArray = (originalArray, num) => {
  const filteredArray = originalArray.map((item) => {
    const keys = Object.keys(item)
    const filteredKeys = keys.filter((key) => key !== 'status')
    return filteredKeys.reduce((acc, key) => {
      acc[key] = item[key]
      return acc
    }, {})
  })
  return splitArrayEvenly(filteredArray, num)
}

/* 手动排班名字变id */
export const nameToId = (allName, existingName) => {
  //对existingName排序
  existingName.sort(sortByFirstCharPinyin)
  // 创建一个映射，以name为键，id为值
  const idMap = new Map(allName.map((item) => [item.name, item.stu_id]))
  // 使用names数组中的name作为键，从映射中获取id
  const ids = existingName
    .map((name) => {
      const id = idMap.get(name)
      return id !== undefined ? id : null // 这里使用null作为占位符，表示找不到的情况
    })
    .filter((id) => id !== null)
  // 返回id数组
  return ids
}

// 拼音排序函数
function sortByFirstCharPinyin(a, b) {
  // 获取每个字符串的首字拼音
  const firstCharPinyinA = pinyin(a.charAt(0), {
    style: pinyin.STYLE_NORMAL,
    heteronym: false
  })[0]
  const firstCharPinyinB = pinyin(b.charAt(0), {
    style: pinyin.STYLE_NORMAL,
    heteronym: false
  })[0]

  // 比较拼音，如果相同则比较原字符串
  if (firstCharPinyinA === firstCharPinyinB) {
    return a.localeCompare(b)
  }
  return firstCharPinyinA.localeCompare(firstCharPinyinB)
}
